package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.mapper.TourismUserMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismPersonnel;
import com.ruoyi.system.service.ITourismPersonnelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 预约人员明细Controller
 *
 * @author gqd
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/system/personnel")
public class TourismPersonnelController extends BaseController
{
    @Autowired
    private ITourismPersonnelService tourismPersonnelService;
    @Autowired
    private TokenService tokenService;
    @Resource
    private TourismUserMapper tourismUserMapper;

    /**
     * 查询预约人员明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:personnel:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismPersonnel tourismPersonnel)
    {
        startPage();
        List<TourismPersonnel> list = tourismPersonnelService.selectTourismPersonnelList(tourismPersonnel);
        return getDataTable(list);
    }

    /**
     * 导出预约人员明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:personnel:export')")
    @Log(title = "预约人员明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismPersonnel tourismPersonnel)
    {
        List<TourismPersonnel> list = tourismPersonnelService.selectTourismPersonnelList(tourismPersonnel);
        ExcelUtil<TourismPersonnel> util = new ExcelUtil<TourismPersonnel>(TourismPersonnel.class);
        return util.exportExcel(list, "personnel");
    }

    /**
     * 获取预约人员明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:personnel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tourismPersonnelService.selectTourismPersonnelById(id));
    }

    /**
     * 新增预约人员明细
     */
    @PreAuthorize("@ss.hasPermi('system:personnel:add')")
    @Log(title = "预约人员明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismPersonnel tourismPersonnel)
    {
        return toAjax(tourismPersonnelService.insertTourismPersonnel(tourismPersonnel));
    }

    /**
     * 修改预约人员明细
     */
    @PreAuthorize("@ss.hasPermi('system:personnel:edit')")
    @Log(title = "预约人员明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismPersonnel tourismPersonnel)
    {
        return toAjax(tourismPersonnelService.updateTourismPersonnel(tourismPersonnel));
    }

    /**
     * 删除预约人员明细
     */
    @PreAuthorize("@ss.hasPermi('system:personnel:remove')")
    @Log(title = "预约人员明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tourismPersonnelService.deleteTourismPersonnelByIds(ids));
    }

    @PostMapping("/insertPerson")
    public AjaxResult insertPerson(HttpServletRequest request, @RequestBody String jsonRequest){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser == null){
            return AjaxResult.error(201,"您还未注册或者登录，请先注册或登录");
        }else {
            TourismUser tourismUser = tourismUserMapper.selectByUserId(loginUser.getUserId());
            if (tourismUser == null || loginUser.getUserId() == null) {
                return AjaxResult.error(201, "您还未注册或者登录，请先注册或登录");
            }else {
                JSONObject jsonObject = JSONObject.parseObject(jsonRequest);
                Map<String, Object> map =jsonObject;
                List<TourismPersonnel> list = (List) map.get("spotName");
                List<TourismPersonnel> shProduct = JSONObject.parseArray(list.toString(),TourismPersonnel.class);
                Integer appointmentId = (Integer) map.get("appointmentId");
                String date = (String) map.get("date");
                Integer personNum = (Integer) map.get("personNum");
                return tourismPersonnelService.insertPerson(loginUser.getUserId(),shProduct,appointmentId,date,personNum);
            }
        }

//        JSONObject jsonObject = JSONObject.parseObject(jsonRequest);
//        Map<String, Object> map =jsonObject;
//        List<TourismPersonnel> list = (List) map.get("spotName");
//        List<TourismPersonnel> shProduct = JSONObject.parseArray(list.toString(),TourismPersonnel.class);
//
//        Integer appointmentId = (Integer) map.get("appointmentId");
//        String date = (String) map.get("date");
//        Integer personNum = (Integer) map.get("personNum");
//        return tourismPersonnelService.insertPerson(9,shProduct,appointmentId,date,personNum);
    }


}
